import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { PerFamilyFinancialApi } from '#/api/per/familyFinancial';
import type { DescriptionItemSchema } from '#/components/description';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { formatDate } from '@vben/utils';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'relation',
      label: '与本人关系',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.PER_RELATION),
        placeholder: '请选择与本人关系',
      },
    },

    {
      fieldName: 'familyName',
      label: '姓名',
      component: 'Input',
      componentProps: {
        placeholder: '请输入姓名',
      },
    },
    {
      fieldName: 'cardType',
      label: '证件类型',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.SYSTEM_CARD_TYPE),
        placeholder: '请选择证件类型',
      },
    },
    {
      fieldName: 'familyIdcard',
      label: '身份证号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入身份证号',
      },
    },
    {
      fieldName: 'productName',
      label: '产品名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入产品名称',
      },
    },
    {
      fieldName: 'holdingAmount',
      label: '持有份额',
      component: 'Input',
      componentProps: {
        placeholder: '请输入持有份额',
      },
    },
    {
      fieldName: 'marketValue',
      label: '当前市值',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入当前市值',
      },
    },
    {
      fieldName: 'recordDate',
      label: '记录时间',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'x',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '人员ID，对应 per_employee.id',
      field: 'employeeId',
    },
    {
      label: '与本人关系（称谓）',
      field: 'relation',
    },
    {
      label: '系统人员ID，对应 system_person.id',
      field: 'systemPersonId',
    },
    {
      label: '家庭成员姓名',
      field: 'familyName',
    },
    {
      label: '证件类型',
      field: 'cardTypeName',
    },
    {
      label: '家庭成员身份证号',
      field: 'familyIdcard',
    },
    {
      label: '产品名称',
      field: 'productName',
    },
    {
      label: '持有份额',
      field: 'holdingAmount',
    },
    {
      label: '当前市值',
      field: 'marketValue',
    },
    {
      label: '记录时间',
      field: 'recordDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '是否有效（1：有效，0：无效）',
      field: 'statusName',
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'relation',
      label: '与本人关系',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.PER_RELATION),
        placeholder: '请选择与本人关系',
      },
    },

    {
      fieldName: 'familyName',
      label: '姓名',
      component: 'Input',
      componentProps: {
        placeholder: '请输入姓名',
      },
    },
    {
      fieldName: 'cardType',
      label: '证件类型',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.SYSTEM_CARD_TYPE),
        placeholder: '请选择证件类型',
      },
    },
    {
      fieldName: 'familyIdcard',
      label: '身份证号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入身份证号',
      },
    },
    {
      fieldName: 'productName',
      label: '产品名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入产品名称',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<PerFamilyFinancialApi.FamilyFinancial>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '主键ID',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'relation',
      title: '与本人关系',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.PER_RELATION },
      },
    },
    {
      field: 'familyName',
      title: '姓名',
      minWidth: 120,
    },
    {
      field: 'cardType',
      title: '证件类型',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.SYSTEM_CARD_TYPE },
      },
    },
    {
      field: 'familyIdcard',
      title: '身份证号',
      minWidth: 120,
    },
    {
      field: 'productName',
      title: '产品名称',
      minWidth: 120,
    },
    {
      field: 'holdingAmount',
      title: '持有份额',
      minWidth: 120,
    },
    {
      field: 'marketValue',
      title: '当前市值',
      minWidth: 120,
    },
    {
      field: 'recordDate',
      title: '记录时间',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 80,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
